package com.tesserate.timecontrol;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;

import com.tesserate.game.api.core.GameCore;
import com.tesserate.game.api.graphics.ImageResource;
import com.tesserate.game.api.graphics.NullRepaintManager;
import com.tesserate.game.api.graphics.Resource;
import com.tesserate.game.api.graphics.ResourceManager;
import com.tesserate.game.api.input.GameAction;
import com.tesserate.game.api.input.InputManager;
import com.tesserate.timecontrol.ui.Lobby;

public class GameClock extends GameCore{
	private InputManager inputManager;
    private GameAction exit;
    private GameAction mouseClick;
    private GameAction start;
    private GameAction troca;
    private GameAction pausa;

    private Lobby lobby = new Lobby();
	
	public static void main(String[] args) {
        new GameClock().run();
    }
    
	@Override
    public void draw(Graphics2D g) {
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		g.setColor(new Color(255, 94, 55));
		g.fillRect(0, 0, screen.getWidth(), screen.getWidth());
		lobby.paint(g);
//		g.setColor(Color.white);
//		g.drawString(inputManager.getMouseX() + ", " + inputManager.getMouseY(), 700, 570);
    }
	
	@Override
	public void update(long elapsedTime){
		if(lobby.getClock().isStarted()){
			lobby.update(elapsedTime);
		}
		
        if (exit.isPressed()) {
            stop();
        }
        
        if (start.isPressed()) {
            lobby.start();
        }
        
        if(pausa.isPressed()){
        	lobby.getClock().pause();
        }
        
        if(troca.isPressed()){
        	lobby.passaAVez();
        }

        if(mouseClick.isPressed()){
        	lobby.passaAVez();
//        	lobby.setMsg(inputManager.getMouseX() + ", " + inputManager.getMouseY());
        }
	}

	@Override
    public void init() {
        super.init();

        // set up input manager
        exit = new GameAction("exit", GameAction.DETECT_INITAL_PRESS_ONLY);
        start = new GameAction("start", GameAction.DETECT_INITAL_PRESS_ONLY);
        troca = new GameAction("troca", GameAction.DETECT_INITAL_PRESS_ONLY);
        pausa = new GameAction("troca", GameAction.DETECT_INITAL_PRESS_ONLY);
        
        mouseClick = new GameAction("mouseClick", GameAction.DETECT_INITAL_PRESS_ONLY);
        inputManager = new InputManager( screen.getFullScreenWindow());
        inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
        inputManager.mapToKey(start, KeyEvent.VK_S);
        inputManager.mapToKey(troca, KeyEvent.VK_SPACE);
        inputManager.mapToKey(pausa, KeyEvent.VK_P);
        inputManager.mapToMouse(mouseClick, InputManager.MOUSE_BUTTON_1);
        
        NullRepaintManager.install();
        Resource image = new ImageResource("lobby", "images/ChessTimer.png");
        ResourceManager.addResource(image);
        lobby.init();
	}
		
}
